local conf = require "hotitem"
local url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.uri;
local id, app, t, ret

--conf:parse(ngx.var.hotitem_rule, ngx.var.hotitem_ver)
if not pcall(function (rule, ver) conf:parse(rule, ver) end, ngx.var.hotitem_rule, ngx.var.hotitem_ver) then
    ngx.log(ngx.ERR, "parse hotitem rule failed:", ngx.var.hotitem_rule, "|", ngx.var.hotitem_ver)
end

--id, app, t = conf:filter(url, ngx.var.args)
--if not pcall(function (pcall_url, args) id, app, t = conf:filter(pcall_url, args) end, url, ngx.var.args) then
if not pcall(function (pcall_url) id, app, t = conf:filter(pcall_url) end, url) then
    ngx.log(ngx.INFO, "filter url failed:", ngx.var.hotitem_rule, "|", ngx.var.hotitem_ver)
end

function build_key()
    if id then
        --workaround for lj_str_new problem of luajit, need remove this on hotitem.lua
        id = math.random(1000000, 9999999) .. id
        ret = id  .. "&" .. app .. "|" .. t ..  ":" .. url
    else
        ret = nil
    end
end

if not pcall(build_key) then
    ngx.log(ngx.ERR, "build return key failed")
end

return ret
